package etc.com.market;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MeatAct extends Activity implements OnItemClickListener {
	/** Called when the activity is first created. */
	private ListView lv;
	ArrayList<String> arrtext = new ArrayList<String>();
	int[] idimage = new int[6];
	int[] idimage1 = new int[6];
	int key;
	private static final int REQUEST_CODE = 10;
	String str;
	String previous;
	String storename;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.listitem);

		lv = (ListView) findViewById(R.id.listview);
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(this);
		lv.setAdapter(new ImageAdapter(this));

		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String messageReceive = extras.getString("message");

		if (messageReceive != null) {
			// arrtext.add(messageReceive);
			str = messageReceive.toString();

			previous = str.substring(0, str.indexOf(";"));
			storename = str.substring(str.indexOf(";") + 1);

			TextView view = (TextView) findViewById(R.id.storename);
			view.setText(storename);
			
			ImageView view1 = (ImageView)findViewById(R.id.headerprevious);
			Resources res = getResources();
			Drawable drawable = res.getDrawable(R.drawable.search_category_s1);
			
			view1.setImageDrawable(drawable);
//			TextView view1 = (TextView) findViewById(R.id.headerprevious);
//			view1.setText(previous);
		}

		TextView v = (TextView) findViewById(R.id.btnNext);
		v.setText("商品検索結果");

		arrtext.add("糖度たっぷり今が旬");
		arrtext.add("糖度たっぷり今が旬2");
		arrtext.add("糖度たっぷり今が旬3");
		arrtext.add("糖度たっぷり今が旬4");

		idimage[0] = R.drawable.product1;
		idimage[1] = R.drawable.product2;
		idimage[2] = R.drawable.product3;
		idimage[3] = R.drawable.product4;

		idimage1[0] = R.drawable.detail;
		idimage1[1] = R.drawable.detail;
		idimage1[2] = R.drawable.detail;
		idimage1[3] = R.drawable.detail;

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				onClick(view,
						("List Products;" + storename + ";"
								+ arrtext.get(position).toString() + ";" + idimage[position]));
			}
		});
	}

	// function use for footer button ==============================
	public void Previous(View view) {
		//onClickSearch(view, ("Cetagory;" + storename));
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void Home(View view) {
		Intent i = new Intent(this, StoreTopPage.class);
		i.putExtra("message", storename);
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void SearchProduct(View view) {
		onClickSearch(view, ("店舗詳細;" + storename));
	}

	public void SpecifyProduct(View view) {
		onClickSpec(view, ("店舗詳細;" + storename));
	}

	public void Info(View view) {
		onClickInfo(view, storename);
	}
	
	public void Contact(View view) {
		onClickContact(view, storename);
	}

	// ===================================================================

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		// Toast.makeText(this, "CHON:"+arg2, Toast.LENGTH_SHORT).show();
		if (key != 2) {

			Intent intent = new Intent(this, ProductDetail.class);
			startActivity(intent);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}
	}

	public void onClick(View view, String message) {
		if (key != 2) {
			Intent i = new Intent(this, ProductDetail.class);
			i.putExtra("message", message);
			// Set the request code to any code you like, you can identify the
			// callback via this code
			startActivity(i);
			overridePendingTransition(R.anim.fadein,
	                R.anim.fadeout);
		}

	}
	public void onClickSpec(View view, String message) {
		Intent i = new Intent(this, ListitemActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickSearch(View view, String message) {
//		Intent i = new Intent(this, SearchOursProducts.class);
//		i.putExtra("message", message);
//		// Set the request code to any code you like, you can identify the
//		// callback via this code
//		startActivity(i);
		finish();
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}

	public void onClickInfo(View view, String message) {
		Intent i = new Intent(this, InfomationAct.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	public void onClickContact(View view, String message) {
		Intent i = new Intent(this, SenmailActivity.class);
		i.putExtra("message", message);
		// Set the request code to any code you like, you can identify the
		// callback via this code
		startActivity(i);
		overridePendingTransition(R.anim.fadein,
                R.anim.fadeout);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("returnKey1")) {
				Toast.makeText(this, data.getExtras().getString("returnKey1"),
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return arrtext.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(mContext);

			if (key == 2) {
				View row = inflater.inflate(R.layout.category, parent, false);

				TextView label = (TextView) row.findViewById(R.id.namefile);
				ImageView icon1 = (ImageView) row.findViewById(R.id.icondetail);

				label.setText(arrtext.get(position));
				icon1.setImageResource(idimage1[position]);

				return row;

			} else {
				View row = inflater.inflate(R.layout.row, parent, false);

				ImageView icon = (ImageView) row.findViewById(R.id.iconfile);
				TextView label = (TextView) row.findViewById(R.id.namefile);
				ImageView icon1 = (ImageView) row.findViewById(R.id.icondetail);

				label.setText(arrtext.get(position));
				// label1.setText(arrtext.get(position));
				icon.setImageResource(idimage[position]);
				icon1.setImageResource(idimage1[position]);

				return row;
			}

		}
	}
}
